﻿@page "/DynamicDataGrid"
@using System.Diagnostics.CodeAnalysis
@using System.Drawing;
@using System.Reflection

@inject IJSRuntime jsRuntime

<style>
    .table-cell-edited {
        position: relative;
    }

        .table-cell-edited::after {
            content: "";
            position: absolute;
            top: 0;
            right: 0;
            width: 0;
            height: 0;
            border-top: 10px solid red;
            border-left: 10px solid transparent;
        }

    .my-class td {
        border-top: 1px solid black;
        border-bottom: 1px solid black;
    }

</style>
<link rel="stylesheet" href="_content/Radzen.Blazor/css/material-base.css">
<link rel="stylesheet" href="_content/Radzen.Blazor/css/standard-base.css">
@* <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css"> *@
<script src="_content/Radzen.Blazor/Radzen.Blazor.js?v=@(typeof(Radzen.Colors).Assembly.GetName().Version)"></script>
<h2>DataGrid</h2>

<RadzenDataGrid EmptyText="dsdsd" @ref=grid Data="@data" TItem="Dictionary<string, object>" ColumnWidth="200px"
                AllowFiltering="false" FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterMode="FilterMode.Advanced" AllowPaging="false" AllowSorting="false"  ColumnsText="">
    <Columns>
        @foreach (var column in columns)
        {
            <RadzenDataGridColumn @key=@column.Key Title="@column.Key" Type="column.Value"
                                  Property="@column.Key">
                <Template>
                    @context[@column.Key]
                </Template>
            </RadzenDataGridColumn>
        }
    </Columns>
</RadzenDataGrid>
<Button Text="DeleteRow" OnClick="DeleteRowClick" />
<Button Text="DeleteColumn" OnClick="DeleteColumnClick" />

@code {

    public List<Dictionary<string, object>> data { get; set; }

    public IDictionary<string, Type> columns { get; set; } = new Dictionary<string, Type>();

    public RadzenDataGrid<Dictionary<string, object>> grid;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        columns = new Dictionary<string, Type>();

        string json = "[{\"Key\": \"VZ_1_mean\", \"Value\": \"-0.13720459580066255nm\", \"Description\": \"VZ1_Depth(mean)\"}, {\"Key\": \"VZ_1_max\", \"Value\": \"0.5119913877260842nm\", \"Description\": \"VZ1_Depth(max)\"}, {\"Key\": \"VZ_1_min\", \"Value\": \"-0.7896224825944955nm\", \"Description\": \"VZ1_Depth(min)\"}, {\"Key\": \"VZ_2_mean\", \"Value\": \"-0.6159895558903625nm\", \"Description\": \"VZ2_Depth(mean)\"}, {\"Key\": \"VZ_2_max\", \"Value\": \"-0.13126487804038334nm\", \"Description\": \"VZ2_Depth(max)\"}, {\"Key\": \"VZ_2_min\", \"Value\": \"-1.7188620152619478nm\", \"Description\": \"VZ2_Depth(min)\"}]";

        var list = System.Text.Json.JsonSerializer.Deserialize<List<KeyValueDto>>(json);
        foreach (var item in list)
        {
            columns.Add(item.Key, item.Value.GetType());
        }

        data = Enumerable.Range(0, 1).Select(i =>
        {
            var row = new Dictionary<string, object>();

            foreach (var column in columns)
            {
                row.Add(
                    column.Key,
                    list.FirstOrDefault(s => s.Key == column.Key)?.Value
                );
            }
            return row;
        }).ToList();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
    }

    public async Task DeleteRowClick()
    {
        data = new List<Dictionary<string, object>>();
    }


    public async Task DeleteColumnClick()
    {
        columns = new Dictionary<string, Type>();
    }


    public class KeyValueDto
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Key { get; set; } = string.Empty;
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        public string Description { get; set; }
    }

}
